cd "~/Dropbox/Shared Folder - J. Ladd & M (1). Meredith/Replication Files/"
cap log close
log using "LogFiles/TableA13.log", replace

clear all
set more off

mat Results = J(28, 10, -9)

use "RawData/GallupDataBJPS.dta", clear

// Drops Unrepresentative Series
drop if drops == "All" | regexm(drops, "Party")
// Drops Obs. w/o Gender
drop if female < 0
// Drops Obs. w/o Weights
drop if final_weight < 0
// Drops Obs. w/o Dem., Ind., or Rep.
keep if party == -1 | party == 0 | party == 1
// Drops Obs. w/o Education
drop if education < 0

// Indicator for Phone Survey
gen phone = (survey ~= 1)

// Sets the Survey Date as the Mid-Point in Field
gen middle_date = .
replace middle_date = floor((start_date + end_date) / 2)
sort middle_date
format middle_date %td

// Codes Dem. as 0, Ind. as 1/2, and Rep. as 1
gen partisan = 0 if party == -1
replace partisan = 1 if party == 1
replace partisan = .5 if party == 0

gen year = year(middle_date)  
gen yearbin = .
replace yearbin = 1 if year >= 1978 & year <= 1982
replace yearbin = 2 if year >= 1983 & year <= 1987
replace yearbin = 3 if year >= 1988 & year <= 1992
replace yearbin = 4 if year >= 1993 & year <= 1997
replace yearbin = 5 if year >= 1998 & year <= 2002
replace yearbin = 6 if year >= 2003 & year <= 2007
replace yearbin = 7 if year >= 2008 & year <= 2012
drop if missing(yearbin)

drop if age == -99 | age == -9
drop if age <= 24
gen decade_birth = floor((year - age) / 10) * 10
gen agebin = .
replace agebin = 1 if decade_birth == 1920
replace agebin = 2 if decade_birth == 1930
replace agebin = 3 if decade_birth == 1940
replace agebin = 4 if decade_birth == 1950
replace agebin = 5 if decade_birth == 1960
drop if missing(agebin)
drop if (yearbin >= 1 & yearbin <= 2) & agebin == 5

gen college = (education == 5)

// In-Person Surveys


forvalues i = 1(1)4 {
forvalues j = 1(1)4 {
gen ageXyear`j'`i'Xnc= (agebin == `j') * (yearbin == `i') * (1 - college)
gen ageXyear`j'`i'Xc = (agebin == `j') * (yearbin == `i') * college
gen ageXyear`j'`i'XncXfemale = (agebin == `j') * (yearbin == `i') * (1 - college) * female
gen ageXyear`j'`i'XcXfemale = (agebin == `j') * (yearbin == `i') * college * female
}
drop ageXyear1`i'Xnc
}

forvalues i = 3(1)4 {
forvalues j = 5(1)5 {
gen ageXyear`j'`i'Xnc= (agebin == `j') * (yearbin == `i') * (1 - college)
gen ageXyear`j'`i'Xc = (agebin == `j') * (yearbin == `i') * college
gen ageXyear`j'`i'XncXfemale = (agebin == `j') * (yearbin == `i') * (1 - college) * female
gen ageXyear`j'`i'XcXfemale = (agebin == `j') * (yearbin == `i') * college * female
}
drop ageXyear3`i'Xnc
}

areg partisan ageXyear* [pweight = final_weight] if phone == 0, robust absorb(series) 
forvalues i = 1(1)4 {
forvalues j = 1(1)4 {
mat Results[2*(`i' - 1) + 1, 2*(`j' - 1) + 1] = _b[ageXyear`j'`i'XcXfemale]
mat Results[2*(`i' - 1) + 2, 2*(`j' - 1) + 1] = -_se[ageXyear`j'`i'XcXfemale]
mat Results[2*(`i' - 1) + 15, 2*(`j' - 1) + 1] = _b[ageXyear`j'`i'XncXfemale]
mat Results[2*(`i' - 1) + 16, 2*(`j' - 1) + 1] = -_se[ageXyear`j'`i'XncXfemale]
}
}

forvalues i = 3(1)4 {
forvalues j = 5(1)5 {
mat Results[2*(`i' - 1) + 1, 2*(`j' - 1) + 1] = _b[ageXyear`j'`i'XcXfemale]
mat Results[2*(`i' - 1) + 2, 2*(`j' - 1) + 1] = -_se[ageXyear`j'`i'XcXfemale]
mat Results[2*(`i' - 1) + 15, 2*(`j' - 1) + 1] = _b[ageXyear`j'`i'XncXfemale]
mat Results[2*(`i' - 1) + 16, 2*(`j' - 1) + 1] = -_se[ageXyear`j'`i'XncXfemale]
}
}

drop ageXyear*

// Phone Surveys

forvalues i = 3(1)7 {
forvalues j = 1(1)5 {
gen ageXyear`j'`i'Xnc= (agebin == `j') * (yearbin == `i') * (1 - college)
gen ageXyear`j'`i'Xc = (agebin == `j') * (yearbin == `i') * college
gen ageXyear`j'`i'XncXfemale = (agebin == `j') * (yearbin == `i') * (1 - college) * female
gen ageXyear`j'`i'XcXfemale = (agebin == `j') * (yearbin == `i') * college * female
}
drop ageXyear1`i'Xnc
}


areg partisan ageXyear* [pweight = final_weight] if phone == 1, robust absorb(series) 
forvalues j = 1(1)5 {
forvalues i = 3(1)7 {
mat Results[2*(`i' - 1) + 1, 2*(`j' - 1) + 2] = _b[ageXyear`j'`i'XcXfemale]
mat Results[2*(`i' - 1) + 2, 2*(`j' - 1) + 2] = -_se[ageXyear`j'`i'XcXfemale]
mat Results[2*(`i' - 1) + 15, 2*(`j' - 1) + 2] = _b[ageXyear`j'`i'XncXfemale]
mat Results[2*(`i' - 1) + 16, 2*(`j' - 1) + 2] = -_se[ageXyear`j'`i'XncXfemale]
}
}

matlist Results

log close
